package pdm.traj.frontend;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import pdm.traj.entity.TrackPoint2;
import pdm.traj.service.TrajDataService;

public class DataReceptionServlet extends HttpServlet {
	TrajDataService trajDataService = null;
	
	@Override
	public void init(ServletConfig sc) throws ServletException {
		super.init(sc);
//		service = TrajDataServiceFactory.getInstance().getService(0);
		ApplicationContext context = new ClassPathXmlApplicationContext("spring-webcontext.xml");
		trajDataService = (TrajDataService) context.getBean("trajDataService", TrajDataService.class);
	}
	
	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		String cmd = request.getParameter("cmd");
		if (cmd != null && cmd != "") {
			//exec cmd
			if ("delall".equals(cmd)) {
				System.out.println("delete all data");
//				trajDataService.deleteAll();
			} 
		} else {
			TrackPoint2 tp2 =  TrackPoint2.parseTP(request.getParameter("x"));
			if (tp2 != null) {
//				trajDataService.update(tp2);
			}
//			out.write(tp2.toString());
//			System.out.println(tp2);
			
		}
		out.close();
	}
}
